package com.liuyi.netty.im.test.modular;

import com.liuyi.netty.im.test.util.ImHelp;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 指标类
 *
 * @author Mr.Fmy
 * @since 2022/1/20
 */
public class Kpi {

    private static AtomicInteger total_send = new AtomicInteger(0);

    private static AtomicInteger total_receive = new AtomicInteger(0);

    private static Map<String, AtomicInteger> sendMap = new ConcurrentHashMap<>();

    private static Map<String, AtomicInteger> receiveMap = new ConcurrentHashMap<>();

    /**
     * 消息发送数
     */
    public static void msgSendNum(int incr) {
        String timeStamp = ImHelp.getTimeStamp();
        if (!sendMap.containsKey(timeStamp)) {
            sendMap.put(timeStamp, new AtomicInteger(0));
        }
        sendMap.getOrDefault(timeStamp, new AtomicInteger(0)).addAndGet(incr);
        total_send.addAndGet(incr);
    }

    public static int getMsgSendNumSum() {
        return total_send.get();
    }

    public static long getMsgSendNum() {
        return sendMap.getOrDefault(ImHelp.getInAdvanceTimeStamp(1), new AtomicInteger(0)).get();
    }

    /**
     * 消息接收数
     */
    public static void msgReceiveNum() {
        String timeStamp = ImHelp.getTimeStamp();
        if (!receiveMap.containsKey(timeStamp)) {
            receiveMap.put(timeStamp, new AtomicInteger(0));
        }
        receiveMap.getOrDefault(timeStamp, new AtomicInteger(0)).incrementAndGet();
        total_receive.incrementAndGet();
    }

    public static long getMsgReceiveNumSum() {
        return total_receive.get();
    }

    public static long getMsgReceiveNum() {
        return receiveMap.getOrDefault(ImHelp.getInAdvanceTimeStamp(1), new AtomicInteger(0)).get();
    }

    public static void clear() {
        total_send = new AtomicInteger(0);
        total_receive = new AtomicInteger(0);
        sendMap.clear();
        receiveMap.clear();
    }

}
